package foo.bar.wiki.web.action.page;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.action.space.EditSpaceActionBean;
import foo.bar.wiki.web.action.space.SpaceActionBean;
import foo.bar.wiki.web.extension.PageTypeConverter;
import foo.bar.wiki.web.nonextension.NullResolution;
import foo.bar.wiki.domain.Page;
import foo.bar.wiki.domain.Space;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.plugins.PluginException;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class RemovePageActionBean extends BaseActionBean {


    private WikiService wikiService;

    @Validate(converter = PageTypeConverter.class)
    private Page page;

    @DefaultHandler
    public Resolution remove_backToEditSpace() throws PluginException {
        String spaceId = page.getSpace().getId();
        _delete();
        return new RedirectResolution(EditSpaceActionBean.class, "view").addParameter("space", spaceId);
    }


    public Resolution remove_backToWikiSpace() throws PluginException {
        String spaceId = page.getSpace().getId();
        _delete();
        return new RedirectResolution(SpaceActionBean.class, "view").addParameter("space", spaceId);
    }


    private void _delete() throws PluginException {
        wikiService.removePage(page);
        getContext().getMessages().add(new SimpleMessage("Page ["+page.getName()+"] removed successfully"));
    }



    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }
}
